@isTest
private class  ContractLinkPriceControllerTest {
    static testMethod void testMethod1() {

       
        Account acc = TestUtils.createAccount();
        insert acc;

    	ProductStructure__c ps = new ProductStructure__c();
        ps.Classify3__c = '试剂';
        insert ps;
        Product__c p1 = TestUtils.createProduct();
        p1.Category0__c = '试剂';
        p1.ProductStructure__c = ps.Id;
        p1.PriceCompany__c = 'MKSY';
        p1.NCCode__c = '000001';
        p1.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        
        Product__c p2 = TestUtils.createProduct();
        p2.Category0__c = '试剂';
        p2.ProductStructure__c = ps.Id;
        p2.PriceCompany__c = 'MKSY';
        p2.NCCode__c = '000002';
        p2.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        insert new Product__c[] {p1, p2};
  
        Price__c pr1 = new Price__c(
            Product__c = p1.Id,
            Company__c = '四川省迈克实业有限公司',
            CompanyBottomPrice__c = 1.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 1.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,
            InstCost__c = 0.5
            
        );
        Price__c pr2 = new Price__c(
            Product__c = p2.Id,
            Company__c = '四川省迈克实业有限公司',
            CompanyBottomPrice__c = 2.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 2.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,
            InstCost__c = 0.5
            
        );
        insert new Price__c[] {pr1,pr2};
        
           /// 日期处理
        Integer m = Date.today().month(); //当前月份
        Integer y = Date.today().year();  //当前年份 
       ///取上月
         if(m == 1){
        m = 12 ;
            y -= 1;
        }else{
        m -= 1;
            
        }
        
        String  month = String.valueOf(m) + '月';
        String  year= String.valueOf(y) + '年';
        AveragePrice__c av1 = new AveragePrice__c(
        Product__c = p1.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month
            
        );
          AveragePrice__c av2 = new AveragePrice__c(
        Product__c = p2.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month
            
        );
        insert new AveragePrice__c[] {av2,av1}; 


        PriceList__c pl = new PriceList__c();
        pl.Product__c = p1.Id;
        pl.SalesCode__c = '05';
        pl.Price__c = 100;
        pl.Account__c = acc.Id;
        insert pl;

        PriceList__c pl2 = new PriceList__c();
        pl2.Product__c = p2.Id;
        pl2.SalesCode__c = '05';
        pl2.Price__c = 100;
        pl2.Account__c = acc.Id;
        insert pl2;


         Contract__c con = new Contract__c(
            Account__c = acc.Id,
            TerminalCustomer__c = acc.Id,
            Vsalestrucode__c = '05'
            


        );
        insert con;

        ContractProduct__c cp1 = new ContractProduct__c(
            Product__c = p1.Id,
            Contract__c = con.Id,
            Only__c = 'cp1'
        );
        insert cp1;

        ContractLinkPriceController clp = new ContractLinkPriceController();
        clp.init();
        clp.conId = con.Id;
        clp.init();

        clp.getContractProducts();
        clp.searchJson =EncodingUtil.urlEncode('{"type":"FromProduct","brand":"","spec":"","cate0":"","cate1":"","nccode":"000002","ptype":"","serialNo":""}', 'UTF-8') ;

        clp.search();

        clp.setNil();
        List<ContractLinkPriceController.ContractProductBean>  rlist = new List<ContractLinkPriceController.ContractProductBean>();
        ContractLinkPriceController.ContractProductBean cpb = new ContractLinkPriceController.ContractProductBean(pl2);
        rlist.add(cpb);
        clp.resJson = EncodingUtil.urlEncode(JSON.serialize(rlist), 'UTF-8');
        clp.save();

      


    }
}